package com.ts.api.module.api.convert.org.context;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.ts.api.common.constant.api.ApiEnum;
import com.ts.api.common.constant.product.ProductPlanVersionMode;
import com.ts.api.exp.ApiMsgException;
import com.ts.api.module.api.convert.core.handler.ApiConvertHandler;
import com.ts.api.module.api.convert.org.convert.ApiOrgPhoneMaskConvert;
import com.ts.api.module.api.convert.org.convert.ApiOrgPhoneMd5Convert;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.product.entity.po.ProductPO;
import com.ts.api.module.product.entity.po.ProductPlanPO;
import com.ts.api.module.product.service.ProductPlanTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *  API机构转换器上下文
 *
 *  @author Pan
 */
@Component
public class ApiOrgConvertContext {

    @Autowired
    private ApiOrgPhoneMd5Convert apiOrgPhoneMd5Convert;
    @Autowired
    private ApiOrgPhoneMaskConvert apiOrgPhoneMaskConvert;
    @Autowired
    private ProductPlanTypeService productPlanTypeService;

    /**
     *  自动获取机构对接处理器，如果不存在则抛出
     *
     *  @param  productPO       主体实体
     *  @param  productPlanPO   主体计划实体
     *  @return ApiOrgSendEventListener
     */
    public ApiConvertHandler getHandler(ProductPO productPO, ProductPlanPO productPlanPO) {
        String key = ClassConvertUtils.toStr(productPO.getId());

        //  机构计划
        if (productPlanTypeService.hasApiOrgPlan(productPO, productPlanPO)) {
            if (ProductPlanVersionMode.MD5.hasVersionMode(productPlanPO.getVersionMode())) {
                ApiOrgPhoneMd5ConvertHandler handler = apiOrgPhoneMd5Convert.getHandler(key);
                if (handler == null) {
                    throw new ApiMsgException(ApiEnum.API_NULL_FAIL.format("API对接-机构计划-无对应-md5解析器", "null"));
                }
                return handler;
            }

            if (ProductPlanVersionMode.PHONE_MASK_8.hasVersionMode(productPlanPO.getVersionMode())) {
                ApiOrgPhoneMaskConvertHandler handler = apiOrgPhoneMaskConvert.getHandler(key);
                if (handler == null) {
                    throw new ApiMsgException(ApiEnum.API_NULL_FAIL.format("API对接-机构计划-无对应-手机号掩码解析器", "null"));
                }
                return handler;
            }
        }
        throw new ApiMsgException(ApiEnum.API_NULL_FAIL.format("API对接-机构计划-无对应-解析器", "null"));
    }

    /**
     *  所有机构-已对接唯一健
     *
     *  @return List
     */
    public List<String> listOrgKey() {
        List<String> unionKeys = CollUtils.newArrayList();

        List<String> md5 = apiOrgPhoneMd5Convert.getUniqueKeys();
        List<String> mask = apiOrgPhoneMaskConvert.getUniqueKeys();

        unionKeys.addAll(md5);
        unionKeys.addAll(mask);
        return unionKeys;
    }
}
